﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.IO;
using System.Data;
using System.Reflection;
using System.Collections.Generic;
using STHPMS1.Common.C_Extension;

namespace STHPMS1.Web.W_Controller
{

    public class ArticleCompare : IComparer<DataRow>
    {
        public int Compare(DataRow x, DataRow y)
        {
            if(Convert.ToDouble( x["起点桩号"]) < Convert.ToDouble( y["起点桩号"]))
            {
                return -1;
            }
            return 1;
        }
    }
    [RoutePrefix("api/rdstandard")]
    public class B_RdstandardPage1Controller : STHPMS1.Web.W_Core.MyController
    {
        #region 业务逻辑
        [Route("")]
        [Route("page/{page?}", Name = "GetrdstandardByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;


            var data = this.Scene.Bll.B_Rdstandard.GetRdStandard(info);// this.Scene.Bll.B_Rdstandard.Get<B_Rdstandard>(exp, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }
        [Route("")]
        public void Post()
        {
            string rdwayid = System.Web.HttpContext.Current.Request.Form["rdwayid"];
            string data = System.Web.HttpContext.Current.Request.Form["data"];
            this.Scene.Bll.B_Rdstandard.SaveRdStandard(rdwayid, data);

        }

        [Route("")]
        public void Delete(B_Rdstandard entity)
        {
            this.Scene.Bll.B_Rdstandard.Remove(entity.Rdwayid);
        }

        [Route("detail")]
        public dynamic GetRdStandardDeatil(string rdwayid)
        {
            return this.Scene.Bll.B_Rdstandard.GetRdStandardDeatil(rdwayid);
        }

        #endregion


        #region 根据划分属性校验数据
        /// <summary>
        /// 校验是否全幅属性
        /// </summary>
        /// <param name="b_Rdstandard"></param>
        /// <param name="xzqy"></param>
        private void CheckSfqfData(DataTable b_Rdstandard, DataTable sfqf, string imptDate)
        {
            List<string> keys = new List<string>();
            //提取路线编号和行车方向进行分组
            foreach (DataRow drKeys in sfqf.Rows)
            {
                string rdid = drKeys[0].ToString();//路线编号
                
                if (!keys.Contains(rdid))
                    keys.Add(rdid);
            }
            foreach (string key in keys)
            {
                
                DataRow[] drs = sfqf.Select("路线编号='" + key + "'", "起点桩号"); //筛选路线编号和行车方向相同的数据

                //检查闭合性
                CheckColseUp(drs, key, "是否全幅");
            }

            //添加数据到实体表
            foreach (DataRow r in sfqf.Rows)
            {

                string _rdid = r[0].ToString();
                double _version = GetVersion(_rdid);//版本号
                double _beginmp = Convert.ToDouble(r[1]);
                int _CHANGETYPE_DICTID = -1;//划分标准
                _CHANGETYPE_DICTID = GetRoadDividedEnum("是否全幅");
                
                string STAND_ID = "BZ_" + _rdid +  "_" + _CHANGETYPE_DICTID + "_" + r[1].ToString();

                string _value = r[3].ToString();//是否全幅           


                DataRow dr = b_Rdstandard.NewRow();
                dr["STAND_ID"] = STAND_ID;
                dr["RDWAYID"] = _rdid;
                dr["CHANGETYPE_DICTID"] = _CHANGETYPE_DICTID;
                dr["BEGINMP"] = _beginmp;
                dr["VALUE"] = _value;
                dr["IMPORTDATE"] = imptDate;
                dr["HIS_VERSION"] = _version;
                dr["DIRECTION_DICTID"] = null;
                b_Rdstandard.Rows.Add(dr);

            }
        }

        /// <summary>
        /// 校验行政区域属性
        /// </summary>
        /// <param name="b_Rdstandard"></param>
        /// <param name="xzqy"></param>
        private void CheckXzqyData(DataTable b_Rdstandard, DataTable xzqy, string imptDate)
        {
            List<string> keys = new List<string>();
            //提取路线编号和行车方向进行分组
            foreach (DataRow drKeys in xzqy.Rows)
            {
                string rdid = drKeys[0].ToString();//路线编号
                
                if (!keys.Contains(rdid))
                    keys.Add(rdid);
            }
            foreach (string key in keys)
            {
                
                DataRow[] drs = xzqy.Select("路线编号='" + key + "'", "起点桩号"); //筛选路线编号和行车方向相同的数据

                //检查闭合性
                CheckColseUp(drs, key, "行政区域");
            }

            //添加数据到实体表
            foreach (DataRow r in xzqy.Rows)
            {

                string _rdid = r[0].ToString();
                double _version = GetVersion(_rdid);//版本号
                double _beginmp = Convert.ToDouble(r[1]);
                int _CHANGETYPE_DICTID = -1;//划分标准
                _CHANGETYPE_DICTID = GetRoadDividedEnum("行政区域");
                
                string STAND_ID = "BZ_" + _rdid +  "_" + _CHANGETYPE_DICTID + "_" + r[1].ToString();
 
                string _value = "";//行政区域对应的编号 
                Dictionary<int, string> dic = this.Scene.Bll.B_Roadway.GetADMDIV();//所有行政区域
                foreach(KeyValuePair<int,string> kv in dic)
                {
                    if(kv.Value.IndexOf(r[3].ToString())>-1 || r[3].ToString().IndexOf(kv.Value)>-1 || kv.Value== r[3].ToString())
                    {
                        _value = kv.Key.ToString();                      
                        break;
                    }
                }
                if (_value == "" || _value == "0") throw new Exception("系统未能找到" + r[3].ToString() + "行政区域");
                              

                DataRow dr = b_Rdstandard.NewRow();
                dr["STAND_ID"] = STAND_ID;
                dr["RDWAYID"] = _rdid;
                dr["CHANGETYPE_DICTID"] = _CHANGETYPE_DICTID;
                dr["BEGINMP"] = _beginmp;
                dr["VALUE"] = _value;
                dr["IMPORTDATE"] = imptDate;
                dr["HIS_VERSION"] = _version;
                dr["DIRECTION_DICTID"] = null;
                b_Rdstandard.Rows.Add(dr);

            }
        }

        /// <summary>
        /// 校验管养单位
        /// </summary>
        /// <param name="b_Rdstandard"></param>
        /// <param name="xzqy"></param>
        /// <param name="imptDate"></param>
        private void CheckGydwData(DataTable b_Rdstandard, DataTable xzqy, string imptDate)
        {
            List<string> keys = new List<string>();
            //提取路线编号和行车方向进行分组
            foreach (DataRow drKeys in xzqy.Rows)
            {
                string rdid = drKeys[0].ToString();//路线编号
                
                if (!keys.Contains(rdid))
                    keys.Add(rdid);
            }
            foreach (string key in keys)
            {
                
                DataRow[] drs = xzqy.Select("路线编号='" + key + "'", "起点桩号"); //筛选路线编号和行车方向相同的数据

                //检查闭合性
                CheckColseUp(drs, key, "管养单位");
            }

            //添加数据到实体表
            foreach (DataRow r in xzqy.Rows)
            {
                string _rdid = r[0].ToString();
                double _version = GetVersion(_rdid);//版本号
                double _beginmp = Convert.ToDouble(r[1]);
                int _CHANGETYPE_DICTID = -1;//划分标准
                _CHANGETYPE_DICTID = GetRoadDividedEnum("管养单位");
                
                string STAND_ID = "BZ_" + _rdid +  "_" + _CHANGETYPE_DICTID + "_" + r[1].ToString();

                string _value = r[3].ToString();//行政区域对应的编号                

                DataRow dr = b_Rdstandard.NewRow();
                dr["STAND_ID"] = STAND_ID;
                dr["RDWAYID"] = _rdid;
                dr["CHANGETYPE_DICTID"] = _CHANGETYPE_DICTID;
                dr["BEGINMP"] = _beginmp;
                dr["VALUE"] = _value;
                dr["IMPORTDATE"] = imptDate;
                dr["HIS_VERSION"] = _version;
                dr["DIRECTION_DICTID"] = null;
                b_Rdstandard.Rows.Add(dr);

            }
        }

        /// <summary>
        /// 技术等级
        /// </summary>
        /// <param name="b_Rdstandard"></param>
        /// <param name="xzqy"></param>
        /// <param name="imptDate"></param>
        private void CheckJsdjData(DataTable b_Rdstandard, DataTable xzqy, string imptDate)
        {
            List<string> keys = new List<string>();
            //提取路线编号和行车方向进行分组
            foreach (DataRow drKeys in xzqy.Rows)
            {
                string rdid = drKeys[0].ToString();//路线编号
                
                if (!keys.Contains(rdid))
                    keys.Add(rdid);
            }
            foreach (string key in keys)
            {
                
                DataRow[] drs = xzqy.Select("路线编号='" + key + "'", "起点桩号"); //筛选路线编号和行车方向相同的数据

                //检查闭合性
                CheckColseUp(drs, key, "技术等级");
            }

            //添加数据到实体表
            foreach (DataRow r in xzqy.Rows)
            {
                string _rdid = r[0].ToString();
                double _version = GetVersion(_rdid);//版本号
                double _beginmp = Convert.ToDouble(r[1]);
                int _CHANGETYPE_DICTID = -1;//划分标准
                _CHANGETYPE_DICTID = GetRoadDividedEnum("技术等级");
                
                string STAND_ID = "BZ_" + _rdid +  "_" + _CHANGETYPE_DICTID + "_" + r[1].ToString();

                string _value = GetRoadClassEnum(r[3].ToString());//技术等级对应的编号                
                if (_value == "") throw new Exception("系统未找到" + r[3].ToString()+"的技术等级");
                DataRow dr = b_Rdstandard.NewRow();
                dr["STAND_ID"] = STAND_ID;
                dr["RDWAYID"] = _rdid;
                dr["CHANGETYPE_DICTID"] = _CHANGETYPE_DICTID;
                dr["BEGINMP"] = _beginmp;
                dr["VALUE"] = _value;
                dr["IMPORTDATE"] = imptDate;
                dr["HIS_VERSION"] = _version;
                dr["DIRECTION_DICTID"] = null;
                b_Rdstandard.Rows.Add(dr);

            }
        }

        /// <summary>
        /// 车道数
        /// </summary>
        /// <param name="b_Rdstandard"></param>
        /// <param name="xzqy"></param>
        /// <param name="imptDate"></param>
        private void CheckCdsData(DataTable b_Rdstandard, DataTable xzqy, string imptDate)
        {
            List<string> keys = new List<string>();
            //提取路线编号和行车方向进行分组
            foreach (DataRow drKeys in xzqy.Rows)
            {
                string rdid = drKeys[0].ToString();//路线编号
                
                if (!keys.Contains(rdid))
                    keys.Add(rdid);
            }
            foreach (string key in keys)
            {
                
                DataRow[] drs = xzqy.Select("路线编号='" + key + "'", "起点桩号"); //筛选路线编号和行车方向相同的数据

                //检查闭合性
                CheckColseUp(drs, key, "车道数");
            }

            //添加数据到实体表
            foreach (DataRow r in xzqy.Rows)
            {
                string _rdid = r[0].ToString();
                double _version = GetVersion(_rdid);//版本号
                double _beginmp = Convert.ToDouble(r[1]);
                int _CHANGETYPE_DICTID = -1;//划分标准
                _CHANGETYPE_DICTID = GetRoadDividedEnum("车道数");
                
                string STAND_ID = "BZ_" + _rdid +  "_" + _CHANGETYPE_DICTID + "_" + r[1].ToString();

                string _value = r[3].ToString();//行政区域对应的编号                

                DataRow dr = b_Rdstandard.NewRow();
                dr["STAND_ID"] = STAND_ID;
                dr["RDWAYID"] = _rdid;
                dr["CHANGETYPE_DICTID"] = _CHANGETYPE_DICTID;
                dr["BEGINMP"] = _beginmp;
                dr["VALUE"] = _value;
                dr["IMPORTDATE"] = imptDate;
                dr["HIS_VERSION"] = _version;
                dr["DIRECTION_DICTID"] = null;
                b_Rdstandard.Rows.Add(dr);

            }
        }

        /// <summary>
        /// 路面宽度
        /// </summary>
        /// <param name="b_Rdstandard"></param>
        /// <param name="xzqy"></param>
        /// <param name="imptDate"></param>
        private void CheckLmkdData(DataTable b_Rdstandard, DataTable xzqy, string imptDate)
        {
            List<string> keys = new List<string>();
            //提取路线编号和行车方向进行分组
            foreach (DataRow drKeys in xzqy.Rows)
            {
                string rdid = drKeys[0].ToString();//路线编号
                
                if (!keys.Contains(rdid))
                    keys.Add(rdid);
            }
            foreach (string key in keys)
            {
                
                DataRow[] drs = xzqy.Select("路线编号='" + key + "'", "起点桩号"); //筛选路线编号和行车方向相同的数据

                //检查闭合性
                CheckColseUp(drs, key, "路面宽度");
            }

            //添加数据到实体表
            foreach (DataRow r in xzqy.Rows)
            {
                string _rdid = r[0].ToString();
                double _version = GetVersion(_rdid);//版本号
                double _beginmp = Convert.ToDouble(r[1]);
                int _CHANGETYPE_DICTID = -1;//划分标准
                _CHANGETYPE_DICTID = GetRoadDividedEnum("路面宽度");
                
                string STAND_ID = "BZ_" + _rdid +  "_" + _CHANGETYPE_DICTID + "_" + r[1].ToString();

                string _value = r[3].ToString();//行政区域对应的编号                

                DataRow dr = b_Rdstandard.NewRow();
                dr["STAND_ID"] = STAND_ID;
                dr["RDWAYID"] = _rdid;
                dr["CHANGETYPE_DICTID"] = _CHANGETYPE_DICTID;
                dr["BEGINMP"] = _beginmp;
                dr["VALUE"] = _value;
                dr["IMPORTDATE"] = imptDate;
                dr["HIS_VERSION"] = _version;
                dr["DIRECTION_DICTID"] = null;
                b_Rdstandard.Rows.Add(dr);

            }
        }

        /// <summary>
        /// 通车时间
        /// </summary>
        /// <param name="b_Rdstandard"></param>
        /// <param name="xzqy"></param>
        /// <param name="imptDate"></param>
        private void CheckTcsjData(DataTable b_Rdstandard, DataTable xzqy, string imptDate)
        {
            List<string> keys = new List<string>();
            //提取路线编号和行车方向进行分组
            foreach (DataRow drKeys in xzqy.Rows)
            {
                string rdid = drKeys[0].ToString();//路线编号
                
                if (!keys.Contains(rdid))
                    keys.Add(rdid);
            }
            foreach (string key in keys)
            {
                
                DataRow[] drs = xzqy.Select("路线编号='" + key + "'", "起点桩号"); //筛选路线编号和行车方向相同的数据

                //检查闭合性
                CheckColseUp(drs, key, "通车时间");
            }

            //添加数据到实体表
            foreach (DataRow r in xzqy.Rows)
            {
                string _rdid = r[0].ToString();
                double _version = GetVersion(_rdid);//版本号
                double _beginmp = Convert.ToDouble(r[1]);
                int _CHANGETYPE_DICTID = -1;//划分标准
                _CHANGETYPE_DICTID = GetRoadDividedEnum("通车时间");
                
                string STAND_ID = "BZ_" + _rdid +  "_" + _CHANGETYPE_DICTID + "_" + r[1].ToString();

                string _value = r[3].ToString();//通车时间对应的编号                

                DataRow dr = b_Rdstandard.NewRow();
                dr["STAND_ID"] = STAND_ID;
                dr["RDWAYID"] = _rdid;
                dr["CHANGETYPE_DICTID"] = _CHANGETYPE_DICTID;
                dr["BEGINMP"] = _beginmp;
                dr["VALUE"] = _value;
                dr["IMPORTDATE"] = imptDate;
                dr["HIS_VERSION"] = _version;
                dr["DIRECTION_DICTID"] = null;
                b_Rdstandard.Rows.Add(dr);

            }
        }

        /// <summary>
        /// 土基类型
        /// </summary>
        /// <param name="b_Rdstandard"></param>
        /// <param name="xzqy"></param>
        /// <param name="imptDate"></param>
        private void CheckTjlxData(DataTable b_Rdstandard, DataTable xzqy, string imptDate)
        {
            List<string> keys = new List<string>();
            //提取路线编号和行车方向进行分组
            foreach (DataRow drKeys in xzqy.Rows)
            {
                string rdid = drKeys[0].ToString();//路线编号
                
                if (!keys.Contains(rdid))
                    keys.Add(rdid);
            }
            foreach (string key in keys)
            {
                
                DataRow[] drs = xzqy.Select("路线编号='" + key + "'", "起点桩号"); //筛选路线编号和行车方向相同的数据

                //检查闭合性
                CheckColseUp(drs, key, "土基类型");
            }

            //添加数据到实体表
            foreach (DataRow r in xzqy.Rows)
            {
                string _rdid = r[0].ToString();
                double _version = GetVersion(_rdid);//版本号
                double _beginmp = Convert.ToDouble(r[1]);
                int _CHANGETYPE_DICTID = -1;//划分标准
                _CHANGETYPE_DICTID = GetRoadDividedEnum("土基类型");
                
                string STAND_ID = "BZ_" + _rdid +  "_" + _CHANGETYPE_DICTID + "_" + r[1].ToString();

                string _value = GetSubgradeEnum(r[3].ToString());//土基类型对应的编号                

                DataRow dr = b_Rdstandard.NewRow();
                dr["STAND_ID"] = STAND_ID;
                dr["RDWAYID"] = _rdid;
                dr["CHANGETYPE_DICTID"] = _CHANGETYPE_DICTID;
                dr["BEGINMP"] = _beginmp;
                dr["VALUE"] = _value;
                dr["IMPORTDATE"] = imptDate;
                dr["HIS_VERSION"] = _version;
                dr["DIRECTION_DICTID"] = null;
                b_Rdstandard.Rows.Add(dr);

            }
        }


        /// <summary>
        /// 路肩类型
        /// </summary>
        /// <param name="b_Rdstandard"></param>
        /// <param name="xzqy"></param>
        /// <param name="imptDate"></param>
        private void CheckLjlxData(DataTable b_Rdstandard, DataTable xzqy, string imptDate)
        {
            List<string> keys = new List<string>();
            //提取路线编号和行车方向进行分组
            foreach (DataRow drKeys in xzqy.Rows)
            {
                string rdid = drKeys[0].ToString();//路线编号
                
                if (!keys.Contains(rdid))
                    keys.Add(rdid);
            }
            foreach (string key in keys)
            {
                
                DataRow[] drs = xzqy.Select("路线编号='" + key + "'", "起点桩号"); //筛选路线编号和行车方向相同的数据

                //检查闭合性
                CheckColseUp(drs, key, "路肩类型");
            }

            //添加数据到实体表
            foreach (DataRow r in xzqy.Rows)
            {
                string _rdid = r[0].ToString();
                double _version = GetVersion(_rdid);//版本号
                double _beginmp = Convert.ToDouble(r[1]);
                int _CHANGETYPE_DICTID = -1;//划分标准
                _CHANGETYPE_DICTID = GetRoadDividedEnum("有无路肩");
                
                string STAND_ID = "BZ_" + _rdid +  "_" + _CHANGETYPE_DICTID + "_" + r[1].ToString();

                string _value = GetShoulderEnum(r[3].ToString());//有无路肩对应的编号                

                DataRow dr = b_Rdstandard.NewRow();
                dr["STAND_ID"] = STAND_ID;
                dr["RDWAYID"] = _rdid;
                dr["CHANGETYPE_DICTID"] = _CHANGETYPE_DICTID;
                dr["BEGINMP"] = _beginmp;
                dr["VALUE"] = _value;
                dr["IMPORTDATE"] = imptDate;
                dr["HIS_VERSION"] = _version;
                dr["DIRECTION_DICTID"] = null;
                b_Rdstandard.Rows.Add(dr);

            }
        }

        /// <summary>
        /// 路肩类型
        /// </summary>
        /// <param name="b_Rdstandard"></param>
        /// <param name="xzqy"></param>
        /// <param name="imptDate"></param>
        private void CheckJtlData(DataTable b_Rdstandard, DataTable jtl, string imptDate)
        {
            List<string> keys = new List<string>();
            //提取路线编号和行车方向进行分组
            foreach (DataRow drKeys in jtl.Rows)
            {
                string rdid = drKeys[0].ToString();//路线编号

                if (!keys.Contains(rdid))
                    keys.Add(rdid);
            }
            foreach (string key in keys)
            {

                DataRow[] drs = jtl.Select("路线编号='" + key + "'", "起点桩号"); //筛选路线编号和行车方向相同的数据

                //检查闭合性
                CheckColseUp(drs, key, "交通量");
            }

            //添加数据到实体表
            foreach (DataRow r in jtl.Rows)
            {
                string _rdid = r[0].ToString();
                double _version = GetVersion(_rdid);//版本号
                double _beginmp = Convert.ToDouble(r[1]);
                double _end = Convert.ToDouble(r[2]);
                int _CHANGETYPE_DICTID = -1;//划分标准
                _CHANGETYPE_DICTID = GetRoadDividedEnum("交通量");

                string STAND_ID = "BZ_" + _rdid + "_" + _CHANGETYPE_DICTID + "_" + r[1].ToString();

                             

                DataRow dr = b_Rdstandard.NewRow();
                dr["STAND_ID"] = STAND_ID;
                dr["RDWAYID"] = _rdid;
                dr["CHANGETYPE_DICTID"] = _CHANGETYPE_DICTID;
                dr["BEGINMP"] = _beginmp;
                dr["VALUE"] = _end; //交通量终点桩号  
                dr["IMPORTDATE"] = imptDate;
                dr["HIS_VERSION"] = _version;
                dr["DIRECTION_DICTID"] = null;
                b_Rdstandard.Rows.Add(dr);

            }
        }

        private double GetVersion(string rdid)
        {
            if (!Common.C_Utils.MyCacheTools.IsCacheExist("v_"+rdid))
            {
                //查询路线编号的起点和终点桩号
                double _version = this.Scene.Bll.B_Roadway.GetNewHistoryVersion(rdid, true);//版本号
                Common.C_Utils.MyCacheTools.SetCache("v_" + rdid, _version, 24);
                return _version;
            }
            else
            {
                double _version = (double)Common.C_Utils.MyCacheTools.GetCache("v_" + rdid);
                return _version;
            }
        }
        /// <summary>
        /// 桩号校验
        /// </summary>
        /// <param name="drs"></param>
        /// <param name="rdid"></param>
        /// <param name="p"></param>
        private void CheckColseUp(DataRow[] drs, string rdid, string p)
        {
            List<B_Roadway> lst = null;
            if(!Common.C_Utils.MyCacheTools.IsCacheExist(rdid))
            {
                //查询路线编号的起点和终点桩号
                lst = this.Scene.Bll.B_Roadway.SearchById<B_Roadway>(rdid);
                Common.C_Utils.MyCacheTools.SetCache(rdid, lst, 24);
            }
            else
            {
                lst = Common.C_Utils.MyCacheTools.GetCache(rdid) as List<B_Roadway>;
            }
           
            if (lst.Count == 0) throw new Exception("路线编号" + rdid + "不存在");
            //drs排序
            List<DataRow> lstSort = new List<DataRow>();
            foreach(DataRow d in drs)
                lstSort.Add(d);
            lstSort.Sort(new ArticleCompare());
            drs = lstSort.ToArray();

            //闭合检查 
            if (Convert.ToDouble(drs[0][1]) != lst[0].Beginmp || Convert.ToDouble(drs[drs.Length - 1][2]) != lst[0].Endmp)
            {
                string msg = string.Format("路线编号{0}的起终点桩号在Excel的{1}属性中对应的起终点桩号不符:实际桩号[{2}-{3}],EXCEL桩号[{4}-{5}]", rdid, p, lst[0].Beginmp, lst[0].Endmp, Convert.ToDouble(drs[0][1]), Convert.ToDouble(drs[drs.Length - 1][2]));
                throw new Exception(msg);
            }
            //两行以上的桩号首尾空白检查
            if (drs.Length > 1)
            {
                int j = 1;
                for (int i = 0; i < drs.Length - 1; i++)
                {
                    if (Convert.ToDouble(drs[i][2]) != Convert.ToDouble(drs[j][1]))
                    {
                        throw new Exception("Excel中路线编号" + rdid + "的起终点桩号在" + p + "属性中首尾不对应");
                    }
                    j++;
                }
            }
        }

        #endregion


        #region 导入处理
        /// <summary>
        /// 上传数据处理
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <returns></returns>
        [Route("saveFile")]
        public HttpResponseMessage PostSaveFile(string file)
        {

            DataTable entity = null;//实体数据
            Excel_BaseImportData exData = null;//导入数据
            if (File.Exists(file))
            {
                try
                {
                    int startRow = 1;//Excel列头位置

                    //获取Excel的DataTable
                    DataTable sfqf = Common.C_Excel.ExcelTool.RenderDataTableFromExcelBySheet(file, startRow, 0);//是否全幅
                    DataTable xzqy = Common.C_Excel.ExcelTool.RenderDataTableFromExcelBySheet(file, startRow, 1);//行政区域
                    DataTable gydw = Common.C_Excel.ExcelTool.RenderDataTableFromExcelBySheet(file, startRow, 2);//管养单位
                    DataTable jsdj = Common.C_Excel.ExcelTool.RenderDataTableFromExcelBySheet(file, startRow, 3);//技术等级
                    DataTable cds = Common.C_Excel.ExcelTool.RenderDataTableFromExcelBySheet(file, startRow, 4);//车道数
                    DataTable lmkd = Common.C_Excel.ExcelTool.RenderDataTableFromExcelBySheet(file, startRow, 5);//路面宽度
                    DataTable tcsj = Common.C_Excel.ExcelTool.RenderDataTableFromExcelBySheet(file, startRow, 6);//通车时间
                    DataTable tjlx = Common.C_Excel.ExcelTool.RenderDataTableFromExcelBySheet(file, startRow, 7);//土基类型
                    DataTable ljlx = Common.C_Excel.ExcelTool.RenderDataTableFromExcelBySheet(file, startRow, 8);//路肩类型
                    DataTable jtl = Common.C_Excel.ExcelTool.RenderDataTableFromExcelBySheet(file, startRow, 9);//交通量
                    //划分标准的DataTable
                    DataTable b_Rdstandard = new DataTable();
                    PropertyInfo[] propertys = GetAllProperty(new B_Rdstandard());
                    foreach (PropertyInfo p in propertys)
                    {
                        b_Rdstandard.Columns.Add(new DataColumn(p.Name.ToUpper()));
                    }
                    string dateTime = DateTime.Now.ToString();
                    //校验
                    CheckSfqfData(b_Rdstandard, sfqf, dateTime);//是否全幅
                    CheckXzqyData(b_Rdstandard, xzqy, dateTime);//行政区域
                    CheckGydwData(b_Rdstandard, gydw, dateTime);//管养单位
                    CheckJsdjData(b_Rdstandard, jsdj, dateTime);//技术等级
                    CheckCdsData(b_Rdstandard, cds, dateTime);//车道数
                    CheckLmkdData(b_Rdstandard, lmkd, dateTime);//路面宽度
                    CheckTcsjData(b_Rdstandard, tcsj, dateTime);//通车时间
                    CheckTjlxData(b_Rdstandard, tjlx, dateTime);//土基类型
                    CheckLjlxData(b_Rdstandard, ljlx, dateTime);//路肩类型
                    CheckJtlData(b_Rdstandard, jtl, dateTime);//交通量
                    //验证Excel表头格式?
                    //entity = GetData(dt, startRow);//将要导入的实体Table
                    entity = b_Rdstandard;
                    exData = new Excel_BaseImportData(new B_Rdstandard());//划分标准实体
                    exData.DtSource = entity;
                    exData.TableName = "B_Rdstandard"; //划分标准实体

                    List<string> keys = new List<string>();
                    foreach(DataRow dr in entity.Rows)
                    {
                        string key = dr[0].ToString();
                        if(!keys.Contains(key))
                        {
                            keys.Add(key);
                        }
                        else
                        {
                            throw new Exception("主键重复:" + key);
                        }
                    }


                    this.Scene.Bll.Excel.ImportData(exData);

                }
                catch (Exception ex)
                {
                    File.Delete(file);//删除上传数据
                    //抛出错误
                    var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content = new StringContent(ex.Message),
                        ReasonPhrase = "Server Error"
                    };
                    throw new HttpResponseException(response);
                }
                finally
                {
                    Common.C_Utils.MyCacheTools.ClearAllCache();
                }
            }
            else
            {
                //抛出错误
                var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent("服务器未能保存上传文件，请联系管理员"),
                    ReasonPhrase = "Server Error"
                };
                throw new HttpResponseException(response);
            }
            var res = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("GetroadwayByPage", new { page = 1 });
            res.Headers.Location = new Uri(uri);

            return res;
        }

        /// <summary>
        /// 反射获取对象的所有属性
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private PropertyInfo[] GetAllProperty(object t)
        {
            Type temp = t.GetType();
            return temp.GetProperties();
        }
        /// <summary>
        /// 获取路线实体的DataTable
        /// </summary>
        /// <param name="excel">导入excel</param>
        /// <param name="startRow">表头位置</param>
        /// <returns></returns>
        private DataTable GetData(DataTable excel, int startRow)
        {
            List<string> keys = new List<string>();//闭合性检查
            Dictionary<int, string> dic = this.Scene.Bll.B_Roadway.GetADMDIV();//行政区域

            DataTable kh = new DataTable();
            PropertyInfo[] propertys = GetAllProperty(new B_Rdstandard());
            foreach (PropertyInfo p in propertys)
            {
                kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
            }
            kh.Columns.Add(new DataColumn("index")); //记录错误行号
            if (excel.Rows.Count == 0)
            {
                throw new Exception("Excel没有数据");
            }
            int index = startRow + 1;
            string _importDate = DateTime.Now.ToString();//导入时间






            //foreach (DataRow r in excel.Rows)
            //{
            //    int _CHANGETYPE_DICTID = -1;//划分标准


            //string STAND_ID = "BZ_" + _rdid +  "_" + _CHANGETYPE_DICTID + "_" + _BEGINMP;
            //if (!keys.Contains(STAND_ID))
            //{
            //    DataRow dr = kh.NewRow();
            //    dr["STAND_ID"] = STAND_ID;
            //    dr["RDWAYID"] = _rdid;
            //    dr["CHANGETYPE_DICTID"] = _CHANGETYPE_DICTID;
            //    dr["BEGINMP"] = _BEGINMP;
            //    dr["VALUE"] = _value;
            //    dr["IMPORTDATE"] = _importDate;
            //    dr["HIS_VERSION"] = _version;
            //    dr["DIRECTION_DICTID"] = null;
            //    kh.Rows.Add(dr);
            //}

            //}




            return kh;
        }

        /// <summary>
        /// 上传返回上传后路径
        /// </summary>
        [Route("uploadFile")]
        public void PostUploadFile()
        {

            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            string uploadPath =
              System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Upload") + "\\";
            string PhysicalPath = "";
            if (hfc.Count > 0)
            {
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                PhysicalPath = uploadPath + hfc[0].FileName;
                hfc[0].SaveAs(PhysicalPath);
            }

            System.Web.HttpContext.Current.Response.Write(PhysicalPath);
            System.Web.HttpContext.Current.Response.End();
        }

        #region 字典定义

        /// <summary>
        /// 行车方向
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        private int GetDirEnum(string dir)
        {
            if (Common.C_Enum.DirectionEnum.All13.GetEnumDescription() == dir)
            {
                return Common.C_Enum.DirectionEnum.All13.GetEnumValue();
            }
            else if (Common.C_Enum.DirectionEnum.Up11.GetEnumDescription() == dir)
            {
                return Common.C_Enum.DirectionEnum.Up11.GetEnumValue();
            }
            else if (Common.C_Enum.DirectionEnum.Down12.GetEnumDescription() == dir)
            {
                return Common.C_Enum.DirectionEnum.Down12.GetEnumValue();
            }
            return -1;
        }

        /// <summary>
        /// 划分标准
        /// </summary>
        /// <param name="divided"></param>
        /// <returns></returns>
        private int GetRoadDividedEnum(string divided)
        {
            if (Common.C_Enum.RdStandardTypeEnum.R200.GetEnumDescription() == divided)//路面结构
            {
                return Common.C_Enum.RdStandardTypeEnum.R200.GetEnumValue();
            }
            else if (Common.C_Enum.RdStandardTypeEnum.R201.GetEnumDescription() == divided)//构造物
            {
                return Common.C_Enum.RdStandardTypeEnum.R201.GetEnumValue();
            }
            else if (Common.C_Enum.RdStandardTypeEnum.R202.GetEnumDescription() == divided)//行政区域
            {
                return Common.C_Enum.RdStandardTypeEnum.R202.GetEnumValue();
            }
            else if (Common.C_Enum.RdStandardTypeEnum.R203.GetEnumDescription() == divided)//管养单位
            {
                return Common.C_Enum.RdStandardTypeEnum.R203.GetEnumValue();
            }
            else if (Common.C_Enum.RdStandardTypeEnum.R204.GetEnumDescription() == divided)//车道数
            {
                return Common.C_Enum.RdStandardTypeEnum.R204.GetEnumValue();
            }
            else if (Common.C_Enum.RdStandardTypeEnum.R205.GetEnumDescription() == divided)//路面宽度
            {
                return Common.C_Enum.RdStandardTypeEnum.R205.GetEnumValue();
            }
            else if (Common.C_Enum.RdStandardTypeEnum.R206.GetEnumDescription() == divided)//土基类型
            {
                return Common.C_Enum.RdStandardTypeEnum.R206.GetEnumValue();
            }
            else if (Common.C_Enum.RdStandardTypeEnum.R207.GetEnumDescription() == divided)//交通量
            {
                return Common.C_Enum.RdStandardTypeEnum.R207.GetEnumValue();
            }
            else if (Common.C_Enum.RdStandardTypeEnum.R208.GetEnumDescription() == divided)//有无路肩
            {
                return Common.C_Enum.RdStandardTypeEnum.R208.GetEnumValue();
            }
            else if (Common.C_Enum.RdStandardTypeEnum.R209.GetEnumDescription() == divided)//通车时间
            {
                return Common.C_Enum.RdStandardTypeEnum.R209.GetEnumValue();
            }
            else if (Common.C_Enum.RdStandardTypeEnum.R210.GetEnumDescription() == divided)//技术等级
            {
                return Common.C_Enum.RdStandardTypeEnum.R210.GetEnumValue();
            }
            else if (Common.C_Enum.RdStandardTypeEnum.R211.GetEnumDescription() == divided) //是否全幅
            {
                return Common.C_Enum.RdStandardTypeEnum.R211.GetEnumValue();
            }
            return -1;
        }

        /// <summary>
        /// 技术等级
        /// </summary>
        /// <param name="rdClass"></param>
        /// <returns></returns>
        private string GetRoadClassEnum(string rdClass)
        {
            if (Common.C_Enum.RoadClassEnum.R160.GetEnumDescription().IndexOf(rdClass)>-1 )
            {
                return Common.C_Enum.RoadClassEnum.R160.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.RoadClassEnum.R161.GetEnumDescription().IndexOf(rdClass) > -1)
            {
                return Common.C_Enum.RoadClassEnum.R161.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.RoadClassEnum.R162.GetEnumDescription().IndexOf(rdClass) > -1)
            {
                return Common.C_Enum.RoadClassEnum.R162.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.RoadClassEnum.R163.GetEnumDescription().IndexOf(rdClass) > -1)
            {
                return Common.C_Enum.RoadClassEnum.R163.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.RoadClassEnum.R164.GetEnumDescription().IndexOf(rdClass) > -1)
            {
                return Common.C_Enum.RoadClassEnum.R164.GetEnumValue().ToString();
            }
            return "";
        }

        /// <summary>
        /// 土基类型
        /// </summary>
        /// <param name="subgrade"></param>
        /// <returns></returns>
        private string GetSubgradeEnum(string subgrade)
        {
            if (Common.C_Enum.SubgradeEnum.S180.GetEnumDescription() == subgrade)
            {
                return Common.C_Enum.SubgradeEnum.S180.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.SubgradeEnum.S181.GetEnumDescription() == subgrade)
            {
                return Common.C_Enum.SubgradeEnum.S181.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.SubgradeEnum.S182.GetEnumDescription() == subgrade)
            {
                return Common.C_Enum.SubgradeEnum.S182.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.SubgradeEnum.S183.GetEnumDescription() == subgrade)
            {
                return Common.C_Enum.SubgradeEnum.S183.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.SubgradeEnum.S184.GetEnumDescription() == subgrade)
            {
                return Common.C_Enum.SubgradeEnum.S184.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.SubgradeEnum.S185.GetEnumDescription() == subgrade)
            {
                return Common.C_Enum.SubgradeEnum.S185.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.SubgradeEnum.S186.GetEnumDescription() == subgrade)
            {
                return Common.C_Enum.SubgradeEnum.S186.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.SubgradeEnum.S187.GetEnumDescription() == subgrade)
            {
                return Common.C_Enum.SubgradeEnum.S187.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.SubgradeEnum.S188.GetEnumDescription() == subgrade)
            {
                return Common.C_Enum.SubgradeEnum.S188.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.SubgradeEnum.S189.GetEnumDescription() == subgrade)
            {
                return Common.C_Enum.SubgradeEnum.S189.GetEnumValue().ToString();
            }
            return "";
        }

        //路肩类型
        private string GetShoulderEnum(string should)
        {
            if (Common.C_Enum.ShoulderEnum.S192.GetEnumDescription() == should)
            {
                return Common.C_Enum.ShoulderEnum.S192.GetEnumValue().ToString();
            }
            else if (Common.C_Enum.ShoulderEnum.S191.GetEnumDescription() == should)
            {
                return Common.C_Enum.ShoulderEnum.S191.GetEnumValue().ToString();
            }
            return "";
        }
        #endregion

        #endregion

    }
}